
const {ccclass, property} = cc._decorator;

@ccclass
export default class Background extends cc.Component {

   scrWidth : number;
   scrHeight : number;
   bgs : cc.Node[];

   @property(cc.Prefab)
   enemyPre : cc.Prefab = null;

   @property
   speed : number = 50;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.scrWidth = cc.view.getCanvasSize().width;
        this.scrHeight = cc.view.getCanvasSize().height;

        this.bgs = this.node.children;
        cc.director.getCollisionManager().enabled = true;
    }

    start () {
        this.schedule(() => {
            this.createEnemy();
        }, 0.3);
    }

    update (dt) {
        this.bgs[0].y -= this.speed * dt;
        this.bgs[1].y -= this.speed * dt;
        if (this.bgs[0].y < -this.scrHeight) {
            this.bgs[0].y = this.bgs[1].y + this.bgs[1].height;
        }

        if (this.bgs[1].y < -this.scrHeight) {
            this.bgs[1].y = this.bgs[0].y + this.bgs[0].height;
        }
    }

    createEnemy() {
        let enemy = cc.instantiate(this.enemyPre);
        enemy.setParent(cc.director.getScene());
    }
}
